package com.app.qbake.module.mine.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;

import com.wind.base.BaseActivity;
import com.wind.base.utils.Navigator;
import com.wind.pingpp.util.PaymentUtil;
import com.app.qbake.R;
import com.app.qbake.module.mine.BuyedGoodsTabFragment;
import com.app.qbake.module.square.event.ReceiveGoodsEvent;
import com.wind.toastlib.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by wind on 2018/10/20.
 */

public class BuyedGoodsActivity extends BaseActivity {

    public static void start(Context context){
        Navigator.navigate(context,BuyedGoodsActivity.class);
    }
    BuyedGoodsTabFragment fragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_container_with_toolbar);
        EventBus.getDefault().register(this);
        replaceFragment(fragment=new BuyedGoodsTabFragment());

    }


    @Override
    protected void setTitle() {
        mTitleBar.setTitle("我买到的");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        PaymentUtil.onActivityResult(requestCode, resultCode, data, new PaymentUtil.PaymentCallback() {
            @Override
            public void onPayReturn(PaymentUtil.PaymentState state, String msg) {
                switch (state) {
                    case SUCCESS:
                        //ToastUtil.showToast(getActivity(),msg);
                        //查询一下
                        showOpLoadingIndicator();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                hideOpLoadingIndicator();
                                handlePingppSuccess();
                            }
                        },1500);


                        break;
                    case CANCEL:
                    case FAIL:
                    case INVALID:
                    case UNKNOWN:
                        hideOpLoadingIndicator();
                        ToastUtil.showToast(getActivity(), msg);
                        break;
                }
            }

        });
    }

    private void handlePingppSuccess() {
        ToastUtil.showToast(getActivity(),"购买成功");
        //刷新
        fragment.refresh();


    }

    @Subscribe
    public void onReceiveGoodsEvent(ReceiveGoodsEvent event){
        fragment.refresh();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
